import java.util.ArrayList;

public class Node {
  
	private Gamestate gamestate;
	private ArrayList<Node> branching;
    private Integer heuristic;
    private int depth;
    private Integer moveX;
    private Integer moveY;
    private int valueAssigned;
    private Node parent;
    private Node bestMove;
    
    public Node(Gamestate gamestate) {
    	this.gamestate = gamestate;
    	branching = new ArrayList<Node>();
    	parent = null;
    }
    
    public Node(Node node, Integer moveX, Integer moveY) throws Exception {	
    	this.moveX = moveX;
    	this.moveY = moveY;
    	parent = node;
    	this.gamestate = new Gamestate(node.getGamestate(), moveX, moveY);
    	branching = new ArrayList<Node>();
    }
    
    public void addBranch(Node n) {
    	branching.add(n);
    }
    
    public ArrayList<Node> getBranches() {
    	return branching;
    }
	
    public Gamestate getGamestate () {
    	return gamestate;
    }
    
    public int getHeuristic (Gamestate.Player p) {
    	if (heuristic == null) {
    		heuristic = new Integer(gamestate.getHeuristic(p));
    	}
    	return heuristic.intValue();
    }
	
    public int getDepth () {
    	return depth;
    }
    
    public void setDepth(int i) {
    	depth = i;
    }
    
    public int getMoveX() throws Exception {
    	if (moveX == null) {
    		throw new Exception("MoveX not initialised.");
    	} 
    	return moveX.intValue();
    }
    
    public int getMoveY() throws Exception {
    	if (moveY == null) {
    		throw new Exception("MoveY not initialised.");
    	}
    	return moveY.intValue();
    }
    
    public int getValueAssigned(){
    	return valueAssigned;
    }
    
    public void setValueAssigned(int v){
    	valueAssigned = v;
    }
    
    public String getStringWithValue(){    	
    	return "[(" + (moveX + 1) + "," + (moveY + 1) + ")," + valueAssigned + "]";
    }
    
    public String getStringWithoutValue(){ 
    	if (depth == 0)
    		return "root";
    	else return "(" + (moveX + 1) + "," + (moveY + 1) + ")";
    }
    
    public String getParentsString(){
    	if (parent != null)
    		return parent.getParentsString() + "," + getStringWithoutValue();
    	else return "root";
    }

	public Node getBestMove() {
		return bestMove;
	}

	public void setBestMove(Node bestMove) {
		this.bestMove = bestMove;
	}
    
}
